package com.youyi.flymewallpager.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

import com.youyi.flymewallpager.R;
import com.youyi.flymewallpager.dialog.NumberDialog;
import com.youyi.flymewallpager.ui.UIBase;
import com.youyi.flymewallpager.ui.UIPalette;

import butterknife.ButterKnife;

/**
 * Created by xzp on 16/7/6.
 */
public class SimpleSettingFragment extends PreferenceFragment {
    ListPreference mModePreference;
    Preference mNumberPreference;
    Preference mBackgroundPreference;
    private SharedPreferences mSimpleSp;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName("simple-settings");
        addPreferencesFromResource(R.xml.simple_settings);
        mModePreference= (ListPreference) findPreference("mode");
        mModePreference.setSummary(mModePreference.getEntry());
        mModePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                String mode=String.valueOf(newValue);
                if(mode.startsWith("Star")){
                    mModePreference.setSummary("星星星");
                }else {
                    mModePreference.setSummary("点点点");
                }
                return true;
            }
        });
        mNumberPreference=findPreference("number");
        mSimpleSp=getPreferenceManager().getSharedPreferences();
        mNumberPreference.setSummary(String.valueOf(mSimpleSp.getInt("number",1)));
        mNumberPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
//                NumberDialog dialog=new NumberDialog();
//                UIBase baseActivity= (UIBase) getActivity();
//                dialog.show(baseActivity.getSupportFragmentManager(),"number");
                AlertDialog.Builder builder=new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle);
                View view= LayoutInflater.from(getActivity()).inflate(R.layout.dialog_number,null);
                final NumberPicker picker= ButterKnife.findById(view,R.id.number_picker);
                picker.setMaxValue(10);
                picker.setMinValue(1);

                picker.setValue(mSimpleSp.getInt("number",1));
                builder.setView(view);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int number=picker.getValue();
                        SharedPreferences.Editor editor=mSimpleSp.edit();
                        editor.putInt("number",number);
                        mNumberPreference.setSummary(String.valueOf(number));
                        editor.commit();

                    }
                });
                builder.show();
                return true;
            }
        });
        mBackgroundPreference=findPreference("background");
        mBackgroundPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivityForResult(new Intent(getActivity(), UIPalette.class),0);
                return true;
            }
        });

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
